using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

using Pegasus.Windows.Forms;
using Pegasus.Windows.Forms.Explorer;
using Pegasus.Windows.Shell;

namespace Pegasus.Sample.ExplorerView
{
	/// <summary>
	/// Windows form for showing the file system.
	/// </summary>
	public partial class FormFileSystem : FormEx
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="FormFileSystem"/> class.
		/// </summary>
		public FormFileSystem()
			: base( "PegasusSampleExplorerVeiw" )
		{
			InitializeComponent();

			ExplorerFiles.SmallImageList = WindowsShell.GetSystemImageList( ImageSize.SMALL );
			ExplorerFiles.LargeImageList = WindowsShell.GetSystemImageList( ImageSize.LARGE );

			// Set Initial State of the Menu
			MenuHiddenItems.Checked = false;

			LoadTree();
		}

		/// <summary>
		/// Handles the Click event of the MenuExit control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void MenuExit_Click( object sender, EventArgs args )
		{
			Close();
		}

		/// <summary>
		/// Handles the DropDownOpening event of the MenuView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void MenuView_DropDownOpening( object sender, EventArgs args )
		{
			MenuLargeIcons.Checked = false;
			MenuSmallIcons.Checked = false;
			MenuList.Checked = false;
			MenuTitle.Checked = false;
			MenuDetails.Checked = false;

			switch( ExplorerFiles.View )
			{
				case View.LargeIcon:
					MenuLargeIcons.Checked = true;
					break;

				case View.SmallIcon:
					MenuSmallIcons.Checked = true;
					break;

				case View.List:
					MenuList.Checked = true;
					break;

				case View.Tile:
					MenuTitle.Checked = true;
					break;

				case View.Details:
					MenuDetails.Checked = true;
					break;
			}
		}

		/// <summary>
		/// Handles the Click event of the MenuRefresh control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void MenuRefresh_Click( object sender, EventArgs args )
		{
			ExplorerFiles.Refresh();
		}

		/// <summary>
		/// Handles the Click event of the any of the Menu item under the MenuView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void MenuViewItem_Click( object sender, EventArgs args )
		{
			switch( ( (ToolStripMenuItem) sender ).Name )
			{
				case "MenuLargeIcons":
					ExplorerFiles.View = View.LargeIcon;
					break;

				case "MenuSmallIcons":
					ExplorerFiles.View = View.SmallIcon;
					break;

				case "MenuList":
					ExplorerFiles.View = View.List;
					break;

				case "MenuTitle":
					ExplorerFiles.View = View.Tile;
					break;

				case "MenuDetails":
					ExplorerFiles.View = View.Details;
					break;
			}
		}

		/// <summary>
		/// Handles the Click event of the MenuHiddenItems control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void MenuHiddenItems_Click( object sender, EventArgs args )
		{
			MenuHiddenItems.Checked = !MenuHiddenItems.Checked;
			ExplorerFiles.Refresh();
		}

		/// <summary>
		/// Handles the OnLoadChildren event of the ExplorerNode control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="args">The <see cref="Pegasus.Windows.Forms.Explorer.ExplorerEventArgs"/> instance containing the event data.</param>
		private void ExplorerNode_OnLoadChildren( object sender, ExplorerEventArgs args )
		{
			FolderItem folder = args.Node.Tag as FolderItem;
			if( folder != null )
			{
				foreach( ShellItem child in folder.Items )
				{
					// If the item is hidden and we are not showing hidden items
					// then just get the next item.
					if( !MenuHiddenItems.Checked && child.IsHidden )
					{
						continue;
					}

					ExplorerNode node = new ExplorerNode();
					node.Tag = child;
					node.Text = child.Name;
					node.ImageIndex = child.ImageIndex;

					if( child is FolderItem )
					{
						node.LoadChildrenOnDemand = true;
						node.LoadChildren += new EventHandler<ExplorerEventArgs>( ExplorerNode_OnLoadChildren );
					}
					else
					{
						node.ShowInTree = false;
					}

					// Add the child node to it parent.
					args.Node.Nodes.Add( node );
				}
			}
		}

		/// <summary>
		/// Loads the tree.
		/// </summary>
		private void LoadTree()
		{
			DesktopItem desktop = WindowsShell.Desktop;

			ExplorerNode node = new ExplorerNode();
			node.Tag = desktop;
			node.Text = desktop.Name;
			node.ImageIndex = desktop.ImageIndex;
			node.LoadChildrenOnDemand = true;

			node.LoadChildren += new EventHandler<ExplorerEventArgs>( ExplorerNode_OnLoadChildren );

			ExplorerFiles.Nodes.Add( node );
		}
	}
}